using System;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Runtime.Serialization;

namespace Crainiate.Data.Persistence
{
    [Serializable]
	public class Aggregate: Column, IAggregate
	{
		private AggregateType _aggregateType;

        //Constructors
        public Aggregate(Type type, string field): base(type, field)
        {
            AggregateType = AggregateType.Unique;
        }

        public Aggregate(Type type, string field, AggregateType aggregate): base(type, field)
        {
            AggregateType = aggregate;
        }

        protected Aggregate(SerializationInfo info, StreamingContext context): base(info, context)
        {
            _aggregateType = (AggregateType) Enum.Parse(typeof(AggregateType), info.GetString("Aggregate"));
        }

        //Properties
		public virtual AggregateType AggregateType
		{
			get
			{
				return _aggregateType;
			}
			set
			{
                _aggregateType = value;
			}
		}

        //Overrides
        public override int GetHashCode()
        {
            StringBuilder builder = new StringBuilder();
            
            builder.Append(PropertyInfo.ToString());
            builder.Append(_aggregateType.ToString());
            
            return builder.ToString().GetHashCode();
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("AggregateType", Convert.ToInt32(AggregateType).ToString());
        }
        
	}
}
